home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / PixelPlotTest / PixelPlotTest.c < prev    next >
C/C++ Source or Header  |  1995-03-13  |  5KB  |  181 lines

  1.  
  2. // PixelPlotTest
  3.  
  4. // By Ingemar Ragnemalm 1994-1995
  5.  
  6. // This demo demonstrates the difference in speed when plotting single pixels
  7. // in different ways. The conclusion is that the fastest method is over 300
  8. // times faster than the toolbox call SetCPixel
  9.  
  10.  
  11. #define    kSizeH 300
  12. #define    kSizeV 40
  13.  
  14.  
  15. /* Standard inits */
  16.  
  17. static void InitToolbox(void)
  18. {
  19.     InitGraf (&qd.thePort);
  20.     InitFonts ();
  21.     FlushEvents (everyEvent,0);
  22.     InitWindows ();
  23.     InitMenus ();
  24.     TEInit ();
  25.     InitDialogs (nil);
  26.     InitCursor ();
  27. } /*InitToolbox*/
  28.  
  29.  
  30. void main(void)
  31. {
  32.     WindowPtr w;
  33.     RGBColor c, saveColor;
  34.     long startTime, endTime;
  35.     short h, v, i;
  36.     Rect r;
  37.     long rowBytes;
  38.     Ptr pixAddr;
  39.     long baseAddr;
  40.     Point    origin;
  41.  
  42.     long rowStartTable[kSizeV+1];
  43.     SignedByte mmuMode;
  44.     Str255    tempStr;
  45.  
  46.     InitToolbox();
  47.  
  48.     SetRect(&r, 100, 100, 400, 301);
  49.     w = NewCWindow(nil, &r, "\pPixel Plotting Speed Test", true, 2, (WindowPtr)-1, false, 0);
  50.     SetPort(w);
  51.  
  52.     GetForeColor(&saveColor);
  53.  
  54.     c.red = 0xffff;
  55.     c.green = 0;
  56.     c.blue = 0xffff;
  57.     startTime = TickCount ();
  58.     for ( h = 0 ; h <= kSizeH ;h++)
  59.         for ( v = 0 ; v <= kSizeV ;v++)
  60.             SetCPixel(h, v, &c);
  61.     endTime = TickCount ();
  62.  
  63. // Report the result
  64.     MoveTo(10, kSizeV + 20);
  65.     NumToString(endTime - startTime, tempStr);
  66.     DrawString("\pSetCPixel test: ");
  67.     DrawString(tempStr);
  68.     DrawString("\p ticks.");
  69.  
  70.     c.red = 0xffff;
  71.     c.green = 0xffff;
  72.     c.blue = 0;
  73.     RGBForeColor(&c);
  74.     startTime = TickCount ();
  75.     for ( h = 0 ; h <= kSizeH ;h++)
  76.         for ( v = 0 ; v <= kSizeV ;v++)
  77.             {
  78. //                RGBForeColor(c);        // You can uncomment this line as a variant
  79.                 MoveTo(h, v);
  80.                 Line(0, 0);
  81.             };
  82.     endTime = TickCount ();
  83.  
  84.     RGBForeColor(&saveColor);
  85. // Report the result
  86.     MoveTo(10, kSizeV + 40);
  87.     NumToString(endTime - startTime, tempStr);
  88.     DrawString("\pMoveTo/LineTo test: ");
  89.     DrawString(tempStr);
  90.     DrawString("\p ticks.");
  91.  
  92.     c.red = 0;
  93.     c.green = 0xffff;
  94.     c.blue = 0xffff;
  95.     RGBForeColor(&c);
  96.     startTime = TickCount ();
  97.     for ( h = 0 ; h <= kSizeH ;h++)
  98.         for ( v = 0 ; v <= kSizeV ;v++)
  99.         {
  100. //                RGBForeColor(c);        // You can uncomment this line as a variant
  101.             SetRect(&r, h, v, h + 1, v + 1);
  102.             PaintRect(&r);
  103.         };
  104.     endTime = TickCount ();
  105.  
  106.     RGBForeColor(&saveColor);
  107. // Report the result
  108.     MoveTo(10, kSizeV + 60);
  109.     NumToString(endTime - startTime, tempStr);
  110.     DrawString("\pSetRect/PaintRect test: ");
  111.     DrawString(tempStr);
  112.     DrawString("\p ticks.");
  113.  
  114.     if ( (**(**GetMainDevice()).gdPMap).pixelSize != 8 )
  115.     {
  116.         MoveTo(10, kSizeV + 80);
  117.         DrawString("\pDirect plotting is only supported in 256 colors/grays.");
  118.     }
  119.     else
  120.     {
  121.         mmuMode = true32b;
  122.  
  123.         rowBytes = ((**(**GetMainDevice()).gdPMap).rowBytes & 0x3fff);
  124.         SetRect(&r, 0, 0, kSizeH, kSizeV);
  125.         ShieldCursor(&r, *(Point *)&(**((CWindowPtr)w)->portPixMap).bounds);
  126.         baseAddr = (long)(**(**GetMainDevice()).gdPMap).baseAddr;
  127.  
  128. /* Find the offset to the window - though I really know it is 100,100! */
  129.         SetPt(&origin, 0, 0);
  130.         LocalToGlobal(&origin);
  131.  
  132.         startTime = TickCount ();
  133.         SwapMMUMode(&mmuMode);
  134.         for ( i = 1 ; i <= 100 ;i++)
  135.             for ( h = 0 ; h <= kSizeH ;h++)
  136.                 for ( v = 0 ; v <= kSizeV ;v++)
  137.                 {
  138.                     pixAddr = (Ptr) (baseAddr + rowBytes * (v + origin.v) + h + origin.h);
  139.                     *pixAddr = 0x70 + i; /*Some pixel value. We can get this through the color table.*/
  140.                 };
  141.         SwapMMUMode(&mmuMode);
  142.         endTime = TickCount ();
  143.  
  144.         ShowCursor ();
  145. // Report the result
  146.         MoveTo(10, kSizeV + 80);
  147.         NumToString(endTime - startTime, tempStr);
  148.         DrawString("\pDirect plotting test: ");
  149.         DrawString(tempStr);
  150.         DrawString("\p ticks/100 times.");
  151.  
  152. /*Optimize by precalculating as much as possible:*/
  153.         for ( v = 0 ; v <= kSizeV ; v++)
  154.             rowStartTable[v] = baseAddr + rowBytes * (v + origin.v) + origin.h;
  155.         ShieldCursor(&r, *(Point *)&(**((CWindowPtr)w)->portPixMap).bounds);
  156.         startTime = TickCount ();
  157.         SwapMMUMode(&mmuMode);                            /*We must be in 32-bit mode*/
  158.         for ( i = 1 ; i <= 100 ;i++)
  159.             for ( h = 0 ; h <= kSizeH ;h++)
  160.                 for ( v = 0 ; v <= kSizeV ;v++)
  161.                     {
  162.                         pixAddr = (Ptr)(rowStartTable[v] + h);
  163.                         *pixAddr = 0x60 + i;        /*Some pixel value. We can get this through the color table.*/
  164.                     };
  165.         SwapMMUMode(&mmuMode);                            /*Back to previous addressing mode*/
  166.         endTime = TickCount ();
  167.         ShowCursor ();                                    /*Balances ShieldCursor*/
  168. // Report the result
  169.         MoveTo(10, kSizeV + 100);
  170.         NumToString(endTime - startTime, tempStr);
  171.         DrawString("\pOpt. direct plotting test: ");
  172.         DrawString(tempStr);
  173.         DrawString("\p ticks/100 times.");
  174.     };
  175.  
  176.     MoveTo(10, kSizeV + 120);
  177.     DrawString("\pClick mouse to exit.");
  178.     while ( ! Button () )
  179.         ;
  180. } /*main*/
  181.